package edu.pku.sei.modeler.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Color;

import edu.pku.notation.runtime.draw2d.RoundedRectangleFigure;
import edu.pku.notation.runtime.draw2d.TextFigure;
import edu.pku.sei.modeler.uml.ResourceFactory;

public class StructuredClassFigure extends CommonFigure {
	public IFigure root;

	public IFigure body;

	public IFigure getBody() {
		return body;
	}

	private TextFigure nameLabel = null;

	public StructuredClassFigure(final Label header) {

		super();

		// ToolbarLayout layout = new ToolbarLayout();
		XYLayout layout = new XYLayout();
		setLayoutManager(layout);
		setForegroundColor(ColorConstants.blue);
		setBorder(new LineBorder(1));

		root = new RoundedRectangleFigure();
		ToolbarLayout rootLayout = new ToolbarLayout();
		rootLayout.setVertical(true);
		root.setLayoutManager(rootLayout);
		root.setForegroundColor(ColorConstants.black);

		if (header.getText().length() > 0) {
			root.add(header);
		}

		nameLabel = new TextFigure();
		nameLabel.setOpaque(false);
		nameLabel.setFont(ResourceFactory.getHeadFont());
		nameLabel.setBorder(new LineBorder(ColorConstants.blue, 1));
		root.add(nameLabel);

		body = new RoundedRectangleFigure();
		XYLayout xyL = new XYLayout();
		body.setLayoutManager(xyL);
		body.setOpaque(true);
		root.add(body);

		add(root);

	}

	public void changeLineColor(Color color) {
		this.setForegroundColor(color);
		((LineBorder)nameLabel.getBorder()).setColor(color);
	}
	
	public TextFigure getNameLabel() {
		return nameLabel;
	}

	public void setBounds(Rectangle rect) {
		super.setBounds(rect);
		if (root != null)
			root.setSize(rect.width, rect.height);
		if (body != null) {
			body.setSize(rect.width, rect.height);
		}
	}
}
